﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;

namespace HangerSupport
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class cmdDeleteSupport : HYNoTranExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)4);
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)117);
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)3);
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			Transaction transaction = null;
			Result result;
			for (;;)
			{
				Autodesk.Revit.DB.View activeView = base.Doc.ActiveView;
				try
				{
					transaction = new Transaction(base.Doc, "删除抗震组件");
					List<Element> list = base.Sel.PickElementsByRectangle(new FilterElemsByFamilyName(new List<string>
					{
						"侧向支撑"
					}), "框选要删除的构件...").ToList<Element>();
					transaction.Start();
					foreach (Element elem in list)
					{
						base.Doc.DeleteElement(elem);
					}
					transaction.Commit();
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
				{
					Autodesk.Revit.DB.View activeView2 = base.Doc.ActiveView;
					bool flag = transaction != null && (int)transaction.GetStatus() == 1;
					if (flag)
					{
						transaction.Commit();
					}
					bool flag2 = RevitVersionFuncs.GetViewName(activeView) == RevitVersionFuncs.GetViewName(activeView2);
					if (flag2)
					{
						result = Autodesk.Revit.UI.Result.Succeeded;
						break;
					}
				}
				catch (Exception ex2)
				{
					YJKMessageBox.Information(ex2.Message.ToString());
					bool flag3 = transaction != null && (int)transaction.GetStatus() != 2;
					if (flag3)
					{
						transaction.RollBack();
					}
					result = Autodesk.Revit.UI.Result.Failed;
					break;
				}
			}
			return result;
		}
	}
}
